<?php


namespace app\miniweb\controller\v1;


use app\common\lib\exception\ApiException;
use app\common\model\Recode;
use app\miniweb\controller\Auth;
use think\Env;
use XznsTools\cache\CacheManage;

class AstroExplain extends Auth
{
    public function _initialize($headerInfo = [])
    {
        $accessToken = input('access_token/s', '');
        $this->headers = ['access-token' => $accessToken];
        parent::_initialize();
    }

    public function index()
    {
        $rid = input('rid/s', '');
        $app_type = input('app_type/s', '');

        if (!$rid)
        {
            throw new ApiException('请求异常');
        }

        $recode = null;
        if ($this->user)
        {//user不为空，说明用户已经登录，并且已经创建了档案
            $recode = (new Recode())->getRecode(['uid' => $this->user['id'], 'id' => $rid]);
        } else if ($app_type == 'WEB')
        {//user为空，说明没有登录，是h5，获取用户的h5档案
            $recode = CacheManage::getInstance()->setCacheConfig('miniapp.html_recode', $rid)->getCache();
        }
        if (!$recode)
        {
            throw new ApiException('当前档案不存在');
        }

        $oss = Env::get('host.photo');

        $astroExplainData = (new \app\web\service\AstroExplain())->getAstroExplain($recode);

        $smrData = $astroExplainData['smrData'];
        $data = $astroExplainData['data'];

        return $this->fetch('../../web/view/astro_explain_new/index', ['smrData' => $smrData, 'oss' => $oss, 'data' => $data]);
    }
}
